package com.cloudminga.database;
import java.io.File;
import java.sql.*;
import javax.swing.JFileChooser;
public class DataBaseHelper {
    Statement stat;
    Connection conn=null;
    ResultSet rs=null;
    String query;
    CallableStatement cst;
    String id_org;
    public void connect()
    {
        try
        {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:jtds:sqlserver://mssql502.ixwebhosting.com/C230527_masterdatabase","C230527_sumit","Globus1234");            
            stat=conn.createStatement();
        }
        catch(Exception e)
        {
         e.printStackTrace();
        }
    }

 public   DataBaseHelper()
    {
       connect();
    }

    public boolean check_login(String email,String pwd)
    {
        String email_id=email;
        String password=pwd;
        boolean result=false;
       
        try
        {
         cst=conn.prepareCall("{call sp_Pomblast_CheckUser(?,?)}");
         cst.setString(1, email_id);
         System.out.println("complete");
         cst.setString(2, password);
         rs=cst.executeQuery();
         if(rs.next()){
             result=true;
         }
         else{
             result=false;
         }

        }
        catch(Exception e)
        {
        e.printStackTrace();
        }
        return result;
    }// End of check_login

    public boolean checkAvailableId(String id)
    {
        boolean result=false;
        String email_id=id;

        try
        {
         cst=conn.prepareCall("{call sp_Pomblast_GetLoginAvailability(?)}");
         cst.setString(1, email_id);
         rs=cst.executeQuery();
         if(rs.next()){
             result=false;
         }
         else{
             result=true;
         }

        }
        catch(Exception e)
        {
        e.printStackTrace();
        }
        return result;
    }


    public boolean checkFileExist(String email,String file)
    {
        boolean result=false;
        String email_id=email;
         connect();
        try
        {
         cst=conn.prepareCall("{call sp_Pomblast_CheckFileExist(?,?,?)}");
         cst.setString(1, email_id);
         cst.setString(2, file);
         cst.setTimestamp(3,new java.sql.Timestamp(new File(file).lastModified()));
         rs=cst.executeQuery();
         if(rs.next()){
             result=true;
         }else{
             result=false;
         }
        }
        catch(Exception e)
        {
          e.printStackTrace();
        }
        return result;
    }

    
    public void getbackupinfo_user(String email)
    {
       try
       {
            String email_id=email;
            cst=conn.prepareCall("{call sp_Pomblast_GetBackupsInfo(?)}");
            cst.setString(1, email_id);
            rs=cst.executeQuery();
            if(rs.next())
            {
                System.out.println("\n\nIn getbackup_info  User's information is :  ");
                System.out.println("Email :  "+rs.getString("Email"));
                System.out.println("Machine  :  "+rs.getString("Machine"));
                System.out.println("MAcId  :  "+rs.getString("MacId"));
                System.out.println("IP   :  "+rs.getString("IP"));
                System.out.println("User Space :  "+rs.getString("UsedSpace"));
            }
            else
            {
                System.out.print("This user id is not vailid \nEnter correct id");
            }
       }
       catch(Exception e)
       {
          e.printStackTrace();
       }

    }




    public void insertIntoBackupTb(
            String email, String MachineName, String MacId,
            String IPAddress, long startTime, String type,
            long duration, String status, int totalfiles,
            long selectedFilesSize, int filesTransfer, long sizeTransfer) 
    {
        connect();
        try{
        cst=conn.prepareCall("{call sp_Pomblast_InsertBackups_tb(?,?,?,?,?,?,?,?,?,?,?,?)}");
        cst.setString(1,email);
        cst.setString(2,MachineName);//new Date(new java.util.Date().getYear(),new java.util.Date().getMonth(),new java.util.Date().getDate()     )
        cst.setString(3,MacId);
        cst.setString(4,IPAddress);
        cst.setTimestamp(5, new java.sql.Timestamp(startTime));
        cst.setString(6,type);
        cst.setLong(7,duration);
        cst.setString(8,status);
        cst.setInt(9,totalfiles);
        cst.setLong(10,selectedFilesSize);
        cst.setInt(11,filesTransfer);
        cst.setLong(12,sizeTransfer);
        cst.executeUpdate();
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

  
    public synchronized void insertIntoBackupDetailTb(String userid,long startTime,String file,long TimeToUpload,String description)
    {
     try{
         JFileChooser filechooser= new JFileChooser();
        file=file.replace("\\","/");        
        java.util.Date d= new java.util.Date();
        cst=conn.prepareCall("{call sp_Pomblast_InsertBackupDetails_tb(?,?,?,?,?,?,?,?)}");
        cst.setString(1,userid);
        cst.setTimestamp(2,new java.sql.Timestamp(startTime));//new Date(new java.util.Date().getYear(),new java.util.Date().getMonth(),new java.util.Date().getDate()     )
        cst.setString(3,file);       
        cst.setLong(4,new File(file).length());
        cst.setLong(5,TimeToUpload);
        cst.setTimestamp(6,new java.sql.Timestamp(new File(file).lastModified()));
        cst.setString(7,filechooser.getTypeDescription(new File(file)));
        cst.setString(8,description);
        cst.executeUpdate();
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
    public void insertIntoRestoreTb(String userid,long startTime,String file,String path)
    {
     try{
        file=file.replace("\\","/");        
        java.util.Date d= new java.util.Date();
        cst=conn.prepareCall("{call sp_Pomblast_InsertRestore_tb(?,?,?,?)}");
        cst.setString(1,userid);
        cst.setTimestamp(2,new java.sql.Timestamp(startTime));//new Date(new java.util.Date().getYear(),new java.util.Date().getMonth(),new java.util.Date().getDate()     )
        cst.setString(3,file);       
        cst.setString(4,path);
        cst.executeUpdate();
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
     
    }


    public static void main(String r[])//
    {
        DataBaseHelper obj=new DataBaseHelper();
     
//        obj.insertData("sun@gmail.com",new java.util.Date().getTime(),"C:\\test.avi",(long)125478);
       // obj.check_login("manish@globussoft.com", "54321");
       // obj.getbackupinfo_user("shailendra@globussoft.com");
        //System.out.println(obj.checkAvailableId("manish@globussoft.com"));

    }

   

    

}

